<?php
namespace app\api\controller\system;

use app\api\controller\Api;
use app\common\config\logic\DictionaryLogic;
use app\common\config\model\ConfigHallShape as ShapeModel;
use app\common\config\model\ConfigStorePackage;
use app\common\config\validate\ConfigHallShapeValidate as ShapeValidate;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use util\Format;

class Shape extends Api
{
    public function index(): \think\response\Json
    {
        $post = $this->request->post();

        $where = [];
        $where[] = ['store_id', '=', $post['store_id']];
        $where[] = ['hall_id', '=', $post['hall_id']];

        $model = new ShapeModel();
        $rows = $model->where($where)->select();

        return success($rows, '获取数据成功');
    }


    public function create(): \think\response\Json
    {
        $post = $this->request->post();
        // 创建的时候清除干扰的信息
        unset($post['id']);
        // 验证提交
        $validate = new ShapeValidate();
        if (!$validate->scene('create')->check($post)) return error([], $validate->getError());

        $model = new ShapeModel();
        $user = $this->request->user;
        $post['operator_id'] = $user['id'];
        try {
            $model->save($post);
        } catch (DbException $e) {
            return error([], $e->getMessage());
        }

        return success([], '添加宴会厅成功');
    }

    public function save(): \think\response\Json
    {
        $post = $this->request->post();

        // 验证提交
        $validate = new ShapeValidate();
        if (!$validate->scene('edit')->check($post)) return error([], $validate->getError());

        $model = new ShapeModel();
        try {
            $row = $model->where('id', $post['id'])->find();
        } catch (DataNotFoundException $e) {
            return error([], '宴会厅已不存在');
        } catch (DbException $e) {
            return error([], $e->getMessage());
        }

        $rs = $row->save($post);

        if ($rs) {
            // 更新缓存
            $dictionaryLogic = new DictionaryLogic();
            $dictionaryLogic->resetItemCache($post['id'], ConfigStorePackage::class);
            return success([], '编辑宴会厅信息成功');
        } else {
            return error([], '编辑宴会厅信息失败');
        }
    }

    public function delete(): \think\response\Json
    {
        $post = $this->request->post();
        // 验证提交
        $validate = new ShapeValidate();
        if (!$validate->scene('delete')->check($post)) return error([], $validate->getError());

        $model = new ShapeModel();
        try {
            $row = $model->where('id', $post['id'])->find();
        } catch (DataNotFoundException $e) {
            return error([], '宴会厅已不存在');
        }

        try {
            $rs = $row->delete();
        } catch (\Exception $e) {
            return error([], '删除失败');
        }

        if ($rs) {
            // 更新缓存
            $dictionaryLogic = new DictionaryLogic();
            $dictionaryLogic->resetItemCache($post['id'], ConfigStorePackage::class);

            return success([], '删除成功');
        } else {
            return error([], '删除失败');
        }
    }
}